package com.hongning.railway.ui.main.fragments.college;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.app.Constant;
import com.hongning.railway.databinding.ActivityVideoplayBinding;
import com.hongning.railway.entity.reqbean.ReqAddPages;
import com.hongning.railway.entity.reqbean.ReqAddScoreBean;
import com.hongning.railway.util.Apputils;
import com.hongning.railway.util.TimeUtils;

import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;

public class VideoPlayActivity extends BaseActivity<ActivityVideoplayBinding, BaseViewModel> {
    private String url;
    private String newsid;
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_videoplay;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
        url=getIntent().getStringExtra(Constant.EXTRA_DATA);
        newsid = getIntent().getStringExtra(Constant.EXTRA_ID);
        if (TextUtils.isEmpty(url))finish();
    }
    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }
    @Override
    public void initData() {
        super.initData();
        binding.layoutTopinclude.tvTopTitle.setText("课程详情");
        binding.layoutTopinclude.ivLeft.setVisibility(View.VISIBLE);
        binding.layoutTopinclude.ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.player.setUp(url
                , "", Jzvd.SCREEN_NORMAL);
        binding.player.startButton.performClick();
//        JzvdStd.startFullscreenDirectly(this,JzvdStd.class,url,"");
        ReqAddScoreBean reqAddScoreBean = new ReqAddScoreBean();
        reqAddScoreBean.setProjectId("001001001003");
        Messenger.getDefault().send(reqAddScoreBean,Constant.ADD_SCORE);
        if (!TextUtils.isEmpty(newsid)){
            ReqAddPages reqAddPages = new ReqAddPages();
            reqAddPages.setNewsId(newsid);
            reqAddPages.setUserId(Apputils.getUserId());
            AppViewModelFactory.getInstance(getApplication()).getmRepository().addPageView(reqAddPages)
                    .compose(RxUtils.schedulersTransformer())
                    .compose(this.bindToLifecycle())
                    .subscribe(new Consumer() {
                        @Override
                        public void accept(Object o) throws Exception {
                            KLog.d("记录添加成功");
                            Messenger.getDefault().sendNoMsg(Constant.LESSON_REFRESH);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {

                        }
                    });
        }
    }
}
